﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Globalization;

namespace smartvoid.secciones
{
    public partial class Calendario : System.Web.UI.Page
    {
        int idGrupo = 0; 

        protected void Page_Load(object sender, EventArgs e)
        {
            // queda marcado el dia actual al cargar la 1ra vez
            calendarioEventos.SelectedDate = DateTime.Today;
            if (Session["Grupo"] != null)
            {
                idGrupo = Int32.Parse(Session["Grupo"].ToString());
            }
        }

        public void calendarioEventos_DayRender(object sender, DayRenderEventArgs e)
        {
                calendarioEventos_CrearLinks(ref e);
                calendarioEventos_CargarEventos(ref e);
        }

        void calendarioEventos_CrearLinks(ref DayRenderEventArgs e)
        {
            e.Cell.Controls.Clear();
            HyperLink h = new HyperLink();
            if ((e.Day.IsOtherMonth.Equals(false)))
            {
                string dia;
                if (e.Day.DayNumberText.Length == 1)
                    dia = "0" + e.Day.DayNumberText;
                else
                    dia = e.Day.DayNumberText;

                h.NavigateUrl = @"NuevoEvento.aspx?Day=" + dia +
                                "&Month=" + e.Day.Date.Month.ToString() +
                                "&Year=" + e.Day.Date.Year.ToString();
                h.Text = e.Day.DayNumberText;
                h.ForeColor = Color.Black;
                if (e.Day.IsToday)
                {
                    h.ForeColor = Color.White;
                }
                h.Font.Underline = false;
            }
            else
            {
                h.Text = e.Day.DayNumberText;
                h.ForeColor = Color.LightGray;
            }
            e.Cell.Width = h.Width = 80;
            e.Cell.Height = h.Height = 75;
            h.ToolTip = e.Day.Date.ToLongDateString();
            e.Cell.Controls.Add(h);

        }

        void calendarioEventos_CargarEventos(ref DayRenderEventArgs e)
        {
            using (var context = new SmartVoidEntities())
            {
                var query = context.ExecuteStoreQuery<Evento>("SELECT * FROM Eventos WHERE e_IDGrupo = " + idGrupo);

                foreach (var obj in query)
                {
                    if (obj.e_FechaInicio.Month == calendarioEventos.VisibleDate.Month &&
                        obj.e_FechaInicio.Year == calendarioEventos.VisibleDate.Year)
                    {
                        if (obj.e_FechaInicio.Day == e.Day.Date.Day && e.Day.IsOtherMonth.Equals(false))
                        {
                            e.Cell.BackColor = System.Drawing.Color.MistyRose;
                        }
                    }
                }
            }
            //calendarioEventos.DataBind();
        }

        protected void calendarioEventos_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
        {
  
        }
    }
}